import numpy as np
import random as rd
import drawNet as dn
import ER
def Local_World(m0, e0, n, m, M):#局域世界
    G = np.empty((n, n))
    _G = ER.ER1(m0, e0)
    deg = np.zeros((n))
    for i in range(0, m0):
        G[i] = _G[i]
        deg[i] = np.sum(G)
    for i in range(m0, n):
        lw = rd.sample(range(i), k = M)
        sump = 0
        for j in lw:
            sump += deg[j]
        cnt = 0
        prep = vis = np.zeros((n))
        p = deg
        p = (M * p) / ((i + 1) * sump)#优先连接
        prep[0] = 0
        for j in range(1, n):
            prep[i] = prep[i - 1] + p[i]
        while(cnt < m):#连m条边
            r = rd.random()
            for j in range(1, n):
                if(r > prep[j]):
                    if(vis[j - 1] == 0):
                        G[i][j - 1] = G[j - 1][i] = 1
                        vis[j - 1] = 1
                        cnt += 1
                        break
        deg[i] = m
        deg = deg + vis#更新度
    return G
dn.drawGraph(Local_World(10, 20, 50, 4, 9))